package io.wongxd.demo

import android.content.Context
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

typealias M = Modifier

@Composable
fun V(number: Number) {
    Box(modifier = Modifier.height(number.toFloat().dp))
}

@Composable
fun H(number: Number) {
    Box(modifier = Modifier.width(number.toFloat().dp))
}

@Composable
fun AppBtn(text: String, onClick: () -> Unit) {
    Button(onClick = {
        onClick.invoke()
    }, modifier = Modifier.padding(top = 20.dp)) {
        Text(text = text)
    }
}

/**
 * 加载网络图片
 */
@Composable
fun rememberPainter(data: String?): Painter = coil.compose.rememberImagePainter(data)

/**
 * 根据图片文件名加载图片
 */
@Composable
fun resourcePainter(imageName: String): Painter = androidx.compose.ui.res.painterResource(
    getDrawableId(imageName, LocalContext.current)
)

private fun getDrawableId(imageName: String, context: Context) = context.resources.getIdentifier(
    imageName, "drawable", context.packageName
)